/**
 * 
 */
package com.pub.pub;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.xml.DOMConfigurator;

import com.pub.constant.Constant;
import com.pub.exception.InitializeException;
import com.pub.exception.NullEnvironmentVariableException;
import com.pub.exception.ConfigureException;

/**
 * @author iling
 *
 */
public class ConfigureFile 
{
	private static String pptFileName;
	private static String log4jFileName;
	
	private static Properties propertie = new Properties();;
	
	/**
	 * 
	 */
	public ConfigureFile() 
	{
		
	}

	public void initLog4j() throws NullEnvironmentVariableException, InitializeException
	{
		String CONFIGURE_FILE_DIR = System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_CONFIGURE.name());
		
		if(CONFIGURE_FILE_DIR == null){
			throw new NullEnvironmentVariableException(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_CONFIGURE);
		}
		
		if (CONFIGURE_FILE_DIR.charAt(CONFIGURE_FILE_DIR.length() - 1) != File.separatorChar){
			CONFIGURE_FILE_DIR = CONFIGURE_FILE_DIR + File.separator;
    	}
		
		log4jFileName = CONFIGURE_FILE_DIR + "log4j.xml";
		
		File configFile = new File(log4jFileName);
		
		if(configFile.exists() && configFile.isFile()){
			DOMConfigurator.configure(log4jFileName);
		}else{
			throw new InitializeException("Failed: initialize log4j from " + log4jFileName);
		}
	}
	
	public static Properties getPropertyFile(String fileName) throws IOException, NullEnvironmentVariableException
	{
		String pptFileName = fileName + ".properties";;
		
		String configRootDir = System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES.name());
		
		if(configRootDir == null){
			throw new NullEnvironmentVariableException(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES);
		}
		
		Properties propertie = new Properties();
        
    	if (configRootDir.charAt(configRootDir.length() -1) != File.separatorChar){
    		configRootDir = configRootDir + File.separator;
    	}
    	
    	FileInputStream inputFile = new FileInputStream(configRootDir + pptFileName);
    	
        propertie.load(inputFile);
        
        inputFile.close();
        
        return propertie;
 	}
	
	public static File getPropertiesFile(String fileName) throws NullEnvironmentVariableException
	{
		String configRootDir;
		
		configRootDir = System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES.name());
		
		if(configRootDir == null){
			throw new NullEnvironmentVariableException(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES);
		}
		
     	if (configRootDir.charAt(configRootDir.length() -1) != File.separatorChar){
    		configRootDir = configRootDir + File.separator;
    	}
		
    	File file = new File(configRootDir + fileName);
    	
        return file;
 	}
	
	public void loadPropertyFile(String fileName) throws IOException, NullEnvironmentVariableException
	{
		pptFileName = fileName + ".properties";;
		
		String configRootDir = System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES.name());
		
		if(configRootDir == null){
			throw new NullEnvironmentVariableException(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES);
		}
       
    	if (configRootDir.charAt(configRootDir.length() - 1) != File.separatorChar){
    		configRootDir = configRootDir + File.separator;
    	}
    	
    	FileInputStream inputFile = new FileInputStream(configRootDir + pptFileName);

        propertie.load(inputFile);
        
        inputFile.close();
 	}
	
	public void loadPropertyFile(Class<?> propertyFile) throws IOException, NullEnvironmentVariableException
	{
		pptFileName = propertyFile.getSimpleName() + ".properties";
		
		String configRootDir = System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES.name());
		
		if(configRootDir == null){
			throw new NullEnvironmentVariableException(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES);
		}
		
    	if (configRootDir.charAt(configRootDir.length() - 1) != File.separatorChar){
    		configRootDir = configRootDir + File.separator;
    	}
    	
    	FileInputStream inputFile = new FileInputStream(configRootDir + pptFileName);
        propertie.load(inputFile);
        
        inputFile.close();
	}
	
	public String getValueStringNotNull(String tag) throws ConfigureException
	{
		if(getValue(tag) == null || getValue(tag).trim().equals("")){
			throw new ConfigureException("not found " + tag + " property in config file " + getPropertyFileName());
    	}else{
    		return getValue(tag).trim();
		}
	}
	
	public int getValueNotNull(String tag) throws ConfigureException
	{
		if(getValue(tag) == null || getValue(tag).trim().equals("") || !getValue(tag).trim().matches("[\\d]+")){
			throw new ConfigureException("not found " + tag + " property in config file " + getPropertyFileName());
    	}else{
    		return Integer.parseInt(getValue(tag).trim());
		}
	}
	
	public double getValueDoubleNotNull(String tag) throws ConfigureException
	{
		if(getValue(tag) == null || getValue(tag).trim().equals("") || !getValue(tag).trim().matches("[\\d.]+")){
			throw new ConfigureException("not found " + tag + " property in config file " + getPropertyFileName());
    	}else{
    		return Double.parseDouble(getValue(tag).trim());
		}
	}
	
	public String getValue(String key)
    {
        return propertie.getProperty(key);
    }
	
	public String getPropertyFileName()
	{
		return pptFileName;
	}
	
	public String getLog4jFileName()
	{
		return log4jFileName;
	}
	
	public static String getSysConfigurePath()
	{
		return System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_CONFIGURE.name());
	}
	
	public static String getSysPropertiesPath()
	{
		return System.getenv(Constant.ENVIRONMENT_VARIABLES.CONFIG_ROOT_PROPERTIES.name());
	}
}
